package com.study.spmongodb.wwj.thread2.char14;

import java.util.Random;
import java.util.stream.IntStream;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/8 22:45
 */
public class CountDownClient {
    public static final Random random = new Random(System.currentTimeMillis());

    public static void main(String[] args) throws InterruptedException {

        final CountDown latch = new CountDown(5);
        System.out.println("准备多线程处理任务");


        IntStream.rangeClosed(1, 5).forEach(i ->
                new Thread(() -> {
                    System.out.println(Thread.currentThread().getName() + " is working");
                    try {
                        Thread.sleep(random.nextInt(1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    latch.down();
                }, String.valueOf(i)).start()
        );

        //await he  wait要注意这点，别写错
        latch.await();
        System.out.println("多线程任务全部结束，准备第二阶段 ");
        System.out.println("…………………………………………");
        System.out.println("FINISHE");

    }
}
